// pages/exercises/index.js
var app=getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    course:null,
    submitObj:{},
    analysis:false,
    showImage:false,
    questionNum:1,
    oindex:0, //这个oindex 作为题目的类型的索引，单选，多选，判断等
    qindex:0, //qindex 就是 questionindex 是题目真正的索引
    qoindex:0, //questinOptionIndex 题目选项的索引
    maxSize:0, // maxSize 题目的最大数
    qmaxSize:0,
    exercisesTitle:[],
    exercisesItems:[],
    exercisesAsk:[],
    exercises:[],
    showButton:false,
    server:app.data.server,
    answerarr:['A','B','C','D','E','正确','错误'],
    correctAnswer:"",
    qestionType:"",
    question:"",
    questionList:"",
    questionOptions:"",
    questionAnalysis:""
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {    
    var _this=this;
    this.setData({
      course:options
    })
    var that=this;
    var data={};
    
    app.paiRequest("/app/course/tcourses/exercisesNew", {parentCode:options.courseHourseId}).then(function(res){
      var arry=res.data;
      if(typeof(res.data)==="string"){
          arry = JSON.parse(res.data)
      }

      var len=arry.maxSize;
      var temp_question = arry.data[_this.data.oindex].childList[_this.data.qindex];
      var temp_questions_list = arry.data[_this.data.oindex];
     
      _this.setData({
        qestionType: arry.data[_this.data.oindex],
        question: temp_question,
        questionList: temp_questions_list,
        maxSize:len,
        //层级结果
        //arry.data[index] 题的大类，单选或多选
        //arry.data[index].childList[index] 具体的题目信息，包含题的标题，选项等
        //arry.data[_this.data.oindex].childList[_this.data.qindex].childList 具体的题的选项集合
        questionOptions:arry.data[_this.data.oindex].childList[_this.data.qindex].childList,
        correctAnswer: temp_question.answerStr,
        questionAnalysis: temp_question.analysis,
     
      })      
      
      


    
            
    })    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  
  
  /**
   * 单选事件
   */
  radioChang:function(e){
    var _this=this;
    var questionId=e.currentTarget.dataset.id;
    var answerId=e.detail.value;
    var course = this.data.course;
    var type = e.currentTarget.dataset.type;
    var options = this.data.course;
    app.paiRequest("/app/course/exercise/record/save", {
      courseId: options.courseId,
      courseHourseId: options.courseHourseId,
      questionId: questionId,
      answerId: answerId,
      questionType: type
    }).then(function (res) {
      var score=res.data.score;
      if(score>0){
        _this.setData({
            analysis: false,//false
            showImage: true
        })
        
      }else{
        _this.setData({
          analysis: true,
          showImage: true
        })
      }
    });

    
    
  },
  /**
   * 多选事件
   * 点击5秒后请求后台校验选项的正确性
   */
  checkboxChange:function(e){
    var questionId = e.currentTarget.dataset.id;
    var answerId = e.detail.value;
    var submitObj = this.data.submitObj;
    submitObj[questionId] = answerId;
    var type = e.currentTarget.dataset.type;
    var options = this.data.course;
    var _this=this;
    
    app.paiRequest("/app/course/exercise/record/savemul", {
      courseId: options.courseId,
      courseHourseId: options.courseHourseId,
      questionId: questionId,
      answerId: answerId,
      questionType: type
    }).then(function (res) {
      var correctAnswer = _this.data.correctAnswer;
      
       

      var score = res.data.score;
       
      if (score>0) {
        _this.setData({
            analysis: false,//false
          showImage: true
        })
      } else {
        _this.setData({
          analysis: true,
          showImage: true
        })
        
      }
    });

    
  },
  /**
   * 提交答案
   */
  submitQuestion:function(e){
    var options = this.data.course;
    app.paiRequest("/app/course/tcourses/userExercises", {
      courseId: options.courseId,
      courseHourseId: options.courseHourseId,
      exercisesId: options.exercisesId
    }).then(function (res) {
      wx.navigateBack({
        delta:1
      })
    })
  },
  nextList: function () {
    this.setData({
      analysis: false,
      showImage: false
    })
    var _this = this;
    //得到当前题型是多选题还是判断题的集合
    var questionList = this.data.questionList;
     
    //获取当前题的所以
    var questionIndex = this.data.qindex;
    //获取当前题的类型
    var quesiontType = this.data.question.courseItem;
  
    var questionLength = this.data.maxSize;
    //获取下一个题的索引
    let temp_questionIndex = questionIndex + 1;
    //获取下一个题的题号，用于显示
    let next_questionNum = _this.data.questionNum + 1;

    var nextQuestion;
    if (next_questionNum === questionLength){
      nextQuestion = questionList.childList[questionLength-1];
      //最后一题，就没有下一题了，直接显示返回
      _this.setData({
        showButton: true,
         showImage: false
      })
    }else{
      nextQuestion = questionList.childList[temp_questionIndex];
       
    }
   
    //如果下一个题是不用类型，索引重置
    if (nextQuestion.courseItem != quesiontType){
      //初始化改类型题下的索引
      _this.setData({
        qindex: 0, 
        questionNum: next_questionNum,
        question:nextQuestion,
        questionOptions:nextQuestion.childList,
        correctAnswer: nextQuestion.answerStr,
        //下一题不显示答案，以防上一题的答案混淆
        showImage: false
      })
    }else{
      _this.setData({
        qindex: temp_questionIndex,
        questionNum: next_questionNum,
        question: nextQuestion,
        questionOptions: nextQuestion.childList,
        correctAnswer: nextQuestion.answerStr,
        //下一题不显示答案，以防上一题的答案混淆
        showImage:false,

      })
    }

    //保留回显，这段逻辑无用了
    var obje;
    if (obje == undefined) {
      
    } else {
      
      //这里是取数据为了做回显
      // app.paiRequest("/app/course/tcourses/exercisesOptions", {
      //   parentCode: questionList[questionIndex + 1].id
      // }).then(function (ress) {
      //   var options = ress.data.data;
      //   var type = ress.data.objType;
      //   var answer = ress.data.answer;
      //   var correctAnswer=[];
      //   var correctStr = "";
      //   //如果是多选
      //   if(type==1) {
      //     for (var i = 0; i < answer.length; i++) {
      //       if(answer[i].answer==1){
      //         var newVar = _this.data.answerarr[i];
      //       }

      //       correctAnswer.push(newVar);

      //     }
      //     correctStr = correctAnswer.join();
      //   }else {
      //     for (var i = 0; i < answer.length; i++) {
      //       if(answer[i].answer==1){
      //         var newVar = _this.data.answerarr[i];
      //         correctStr = newVar;
      //       }

      //     }
      //     //除了多选，这里就是单选，不管是语音还是案例

      //   }



      //   for (var i = 0; i < options.length; i++) {
      //     options[i].checked = false;
      //   }
      //   _this.setData({
      //     questionOptions: options,
      //     correctAnswer:correctStr
      //   })
      // })
    }
  }
})